import { Controller } from 'egg';

// 分组
const sortGroupRule = {
  list: {
    type: 'array',
    required: true,
    allowEmpty: false,
  },
};
const createGroupRule = {
  formGroup: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
  groupName: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
};
const renameGroupRule = {
  formGroup: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
  groupName: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
};
const removeGroupRule = {
  formGroup: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
};
// 表单
const sortformRule = {
  list: {
    type: 'array',
    required: true,
    allowEmpty: false,
  },
};
const getformListRule = {
  formGroup: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
};
const createformRule = {
  schemaCode: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
  schemaName: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
  formGroup: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
  formProperty: {
    type: 'object',
    required: true,
    allowEmpty: true,
  },
  formCards: {
    type: 'object',
    required: true,
    allowEmpty: true,
  },
  // schemaIcon: {
  //   type: 'string',
  //   required: true,
  //   allowEmpty: false,
  // },
  // dataTitle: {
  //   type: 'array',
  //   required: true,
  //   allowEmpty: true,
  // },
  // userReadScope: {
  //   type: 'string',
  //   required: true,
  //   allowEmpty: false,
  // },
  // isShowComment: {
  //   type: 'boolean',
  //   required: true,
  //   allowEmpty: false,
  // },
  // isShowTaskNotice: {
  //   type: 'boolean',
  //   required: true,
  //   allowEmpty: false,
  // },
  // isShowActionLog: {
  //   type: 'boolean',
  //   required: true,
  //   allowEmpty: false,
  // },
  // displaySetting: {
  //   type: 'object',
  //   required: true,
  //   allowEmpty: false,
  // },
  pos: {
    type: 'number',
    required: true,
    allowEmpty: false,
  },
};
const renameformRule = {
  schemaCode: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
  schemaName: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
};
const removeformRule = {
  schemaCode: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
};
const saveformRule = {
  schemaCode: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
  schemaName: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
  formProperty: {
    type: 'object',
    required: true,
    allowEmpty: true,
  },
  formCards: {
    type: 'object',
    required: true,
    allowEmpty: true,
  },
};
const getformRule = {
  schemaCode: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
};
const createFormBizRule = {
  schemaCode: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
  bizObjectId: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
  data: {
    type: 'object',
    required: true,
    allowEmpty: false,
  },
  status: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
};
const getFormBizRule = {
  schemaCode: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
};
const removeFormBizRule = {
  bizObjectIds: {
    type: 'array',
    required: true,
    allowEmpty: false,
  },
};
const saveFormBizRule = {
  schemaCode: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
  bizObjectId: {
    type: 'string',
    required: true,
    allowEmpty: false,
  },
  status: {
    type: 'string',
    required: true,
    allowEmpty: true,
  },
  data: {
    type: 'object',
    required: true,
    allowEmpty: true,
  },
};

export default class form extends Controller {
  // 表单分组
  async sortGroup() {
    const { ctx, service, logger } = this;
    const params = Object.assign({}, ctx.request.body);
    try {
      ctx.validate(sortGroupRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.sortGroup(params);
    ctx.body = ctx.response.success(result);
  }

  async getGroupList() {
    const { ctx, service } = this;
    const params = Object.assign({}, ctx.query, ctx.params);
    const result = await service.form.getGroupList(params);
    ctx.body = ctx.response.success(result);
  }

  async createGroup() {
    const { ctx, service, logger } = this;
    const params = Object.assign({}, ctx.request.body);
    try {
      ctx.validate(createGroupRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.createGroup(params);
    ctx.body = ctx.response.success(result);
  }

  async renameGroup() {
    const { ctx, service, logger } = this;
    const params = Object.assign({}, ctx.request.body);
    try {
      ctx.validate(renameGroupRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.renameGroup(params);
    ctx.body = ctx.response.success(result);
  }

  async removeGroup() {
    const { ctx, service, logger } = this;
    const params = Object.assign({}, ctx.request.body);
    try {
      ctx.validate(removeGroupRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    if (params.formGroup === 'defaultgroup') {
      ctx.body = ctx.response.secureError('默认分组不可删除');
    } else {
      const result = await service.form.removeGroup(params);
      ctx.body = ctx.response.success(result);
    }
  }

  // 表单
  async sortForm() {
    const { ctx, service, logger } = this;
    const params = Object.assign({}, ctx.request.body);
    try {
      ctx.validate(sortformRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.sortForm(params);
    ctx.body = ctx.response.success(result);
  }

  async getFormList() {
    const { ctx, service, logger } = this;
    const params = Object.assign({}, ctx.query, ctx.params);
    try {
      ctx.validate(getformListRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.getFormList(params);
    ctx.body = ctx.response.success(result);
  }

  async createForm() {
    const { ctx, service, logger } = this;
    const params = Object.assign({}, ctx.request.body);
    try {
      ctx.validate(createformRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.createForm(params);
    ctx.body = ctx.response.success(result);
  }

  async renameForm() {
    const { ctx, service, logger } = this;
    const params = Object.assign({}, ctx.request.body);
    try {
      ctx.validate(renameformRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.renameForm(params);
    ctx.body = ctx.response.success(result);
  }

  async removeForm() {
    const { ctx, service, logger } = this;
    const params = Object.assign({}, ctx.request.body);
    try {
      ctx.validate(removeformRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.removeForm(params);
    ctx.body = ctx.response.success(result);
  }

  async saveForm() {
    const { ctx, service, logger } = this;
    const params = ctx.request.body;
    try {
      ctx.validate(saveformRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.saveForm(params);
    ctx.body = ctx.response.success(result);
  }

  async getFormData() {
    const { ctx, service, logger } = this;
    const params = Object.assign({}, ctx.query, ctx.params);
    try {
      ctx.validate(getformRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.getFormData(params);
    ctx.body = ctx.response.success(result);
  }

  async createFormBiz() {
    const { ctx, service, logger } = this;
    const params = Object.assign({}, ctx.request.body);
    try {
      ctx.validate(createFormBizRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.createFormBiz(params);
    ctx.body = ctx.response.success(result);
  }

  async getFormBiz() {
    const { ctx, service, logger } = this;
    const params = Object.assign({}, ctx.query, ctx.params);
    try {
      ctx.validate(getFormBizRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.getFormBiz(params);
    ctx.body = ctx.response.success(result);
  }

  async removeFormBiz() {
    const { ctx, service, logger } = this;
    const params = Object.assign({}, ctx.request.body);
    try {
      ctx.validate(removeFormBizRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.removeFormBiz(params);
    ctx.body = ctx.response.success(result);
  }

  async saveFormBiz() {
    const { ctx, service, logger } = this;
    const params = ctx.request.body;
    try {
      ctx.validate(saveFormBizRule, params);
    } catch (err: any) {
      logger.error(new Error(err.code));
      ctx.body = ctx.response.paramError(err.errors);
      return false;
    }
    const result = await service.form.saveFormBiz(params);
    ctx.body = ctx.response.success(result);
  }
}
